package com.leetcode.array.removeElement.demo3;

/**
 * @Author Zan
 * @Create 2024/5/8 11:35
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/move-zeroes/  移动零
 */
public class Test {
    public static void main(String[] args) {

    }
}

// 将规则的元素都放到前面来，后面的元素置0即可
class Solution {
    public void moveZeroes(int[] nums) {
        int slow = 0;
        int size = nums.length;
        for (int fast = 0; fast < size; fast++) {
            if (nums[fast] != 0) {
                nums[slow] = nums[fast];
                slow++;
            }
            for (; slow < size; slow++) {
                nums[slow] = 0;
            }
        }
    }
}

class Solution2 {
    public void moveZeroes(int[] nums) {
        int slow = 0, fast = 0;
        int size = nums.length;
        while (fast < size) {
            if (nums[fast] != 0) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        while (slow < size) {
            nums[slow] = 0;
            slow++;
        }
    }
}